package calabashandroidplugin.utils;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Executor {
	private Scanner scanner;
	
	public Scanner execute(String command, String pathToApk, String projectPath) {
		ProcessBuilder builder = new ProcessBuilder("cmd", "/c",
				"calabash-android", command, pathToApk);
		builder.directory(new File(projectPath));
		Process p;

		scanner = null;
		try {
			p = builder.start();
			scanner = new Scanner(p.getInputStream()).useDelimiter("\\Z");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return scanner;
	}
	
	public Scanner execute(String command, String projectPath) {
		ProcessBuilder builder = new ProcessBuilder(command);
		builder.directory(new File(projectPath));
		Process p;

		scanner = null;
		try {
			p = builder.start();
			scanner = new Scanner(p.getInputStream()).useDelimiter("\\Z");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return scanner;
	}
	
	
	public void closeScanner(){
		this.scanner.close();
	}
	
}
